package com.tth.learn.addproperties.java;

import org.junit.Test;

import java.util.*;

/**
 * @Author tth
 * @Date 2022/12/12 11:23
 */
public class CollectionTest {
    @Test
    public  void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(new String("hello"));
        coll.add(new Person("Jerry", 18));
        coll.add(new Student("XiaoMing", 10, 80));

        System.out.println(coll.contains(123));
        System.out.println(coll.contains("hello"));
        System.out.println(coll.contains(new Person("Jerry", 18)));
        System.out.println(coll.contains(new Student("XiaoMin1g", 11, 80)));

        System.out.println("*******************************");
        Collection coll1 = Arrays.asList(123, 456);
        System.out.println(coll.retainAll(coll1));
        System.out.println(coll);
    }

    @Test
    public void test2() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(new String("hello"));
        coll.add(new Person("Jerry", 18));
        coll.add(new Student("XiaoMing", 10, 80));

        System.out.println(coll.hashCode());

        System.out.println("==================集合转数组=================");
        Object[] objects = coll.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);
        }

        System.out.println("==================数组转集合=================");
        List list = Arrays.asList(new String[]{"AA", "BB", "CC"});
        System.out.println(list);

        List ints = Arrays.asList(new int[]{1, 2, 3});
        System.out.println(ints);
    }
}

class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
class Student extends Person {
    private int score;

    public Student(String name, int age, int score) {
        super(name, age);
        this.score = score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return score == student.score && getName().equals(student.getName()) && getAge() == student.getAge();
    }

    @Override
    public int hashCode() {
        return Objects.hash(score);
    }
}
